package com.yhl.admin.controller;

import com.yhl.admin.bean.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

/**
 * @author yhl
 * @create 2021-06-18 14:33
 */
@Slf4j
@Controller
public class IndexController {

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("/widgets.html")
    public String widgets(){
        return "/widgets";
    }
    @GetMapping("/typography.html")
    public String typography(){
        return "/typography";
    }
    @GetMapping("/tabs-accordions.html")
    public String ta(){
        return "/tabs-accordions";
    }
    @GetMapping("/slider.html")
    public String slider(){
        return "/slider";
    }

    @GetMapping("/panels.html")
    public String panels(){
        return "/panels";
    }

    @GetMapping("/general.html")
    public String general(){
        return "/general";
    }

    @GetMapping("/buttons.html")
    public String buttons(){
        return "/buttons";
    }

    /**
     * 登录页面
     */
    @GetMapping(value =  {"/","/login"})
    public String loginPage(){
        log.info("loginPage{} 执行了");
        return "login";
    }

    /**
     * 主页面
     */
    @PostMapping("/login")
    public String main(User user, HttpSession session, Model model){

        //判断用户名和密码是否正确
        if (user.getUserName() != null && "123456".equals(user.getPassword())){
            session.setAttribute("loginUser", user);//登录成功
            return "redirect:/main.html";//请求重定向===> 处理表单重复提交问题
        }else{
            model.addAttribute("msg", "密码不正确！");
            return "login";
        }
    }

    /**
     *
     * 使用请求重定向解决表单重复提交的问题
     */
    @GetMapping("/main.html")
    public String mainPage(User user, HttpSession session, Model model){

        //获取能操作String类型的对象
        ValueOperations<String, String> operations = redisTemplate.opsForValue();

        String s1 = operations.get("/sql");
        String s2 = operations.get("/main.html");

        model.addAttribute("sqlCount", s1);
        model.addAttribute("mainCount", s2);

//        //判断用户是否登录
//        if (session.getAttribute("loginUser") != null){
//
//        }
//
//        //如果用户没有登录返回登录页面
//        model.addAttribute("msg", "用户还没有登录！");
//        return "login";
        return "main";
    }
}
